<template>
	<div class="content" v-loading="mainLoading">
		<h2>
			<span>推荐商品管理</span>
		</h2>
		<div class="content-wrap">
			<!--选择商品-->
			<div class="ct-list">
				<s-wait-goods-upload :limit="999" :batch="true"></s-wait-goods-upload>
			</div>
			<!--提交-->
			<div class="submit">
				<el-button type="primary" @click="saveData">
					<span>保存</span>
				</el-button>
			</div>
		</div>
	</div>
</template>

<script>
	import waitGoodsUpload from "../../components/wait_goods_upload/wait_goods_upload.vue"
	export default {
		components: {
			sWaitGoodsUpload: waitGoodsUpload
		},
		data() {
			return {
				//========其他参数========//
				mainLoading: false,						//提交活动的时候的小圆圈
				loading: true 							//加载小圆圈触发变量
			};
		},
		created() { 
			this.loadData();
		},
		methods: {
			loadData() {
				var url = `${this.url}/commodity/item_get_suggest_items.action`;
				var params = {
				};
				this.$http.get(url,{ params }).then((res, err) => {
					if( res.data.success ) {
						var r = res.data.result;
						r.rows.forEach((val,index)=>{
							this.$store.commit({
								type: "saveGoods/set",
								status: 1,
								commodity_id: val.commodity_id,
								commodity_no: val.commodity_no,
								commodity_name: val.commodity_name
							});
						});
					}else {
						this.loading = false;
						this.$message({
							type: "error",
							message: res.data.result.errmsg
						});
					}
				});
				
			},
			//=======================================发送一次完整的活动给服务器================================//
			saveData() {
				var goods = [];
				var commodity_map = this.$store.state.saveGoods.goodsStatus;
				for(var item in commodity_map) {
					if (commodity_map[item].status === 1) {
						goods.push(commodity_map[item].commodity_id);
					}
				}
				goods = goods.join(",");	
				var params = {
					ids: goods
				};
				this.mainLoading = true;
				var url = `${this.url}/commodity/item_save_suggest_items.action`;
				this.$http.get(url, { params }).then((res) => {
					this.mainLoading = false;
					if( res.data.success ) {
						this.$message({
							type: "success",
							message: '保存成功'
						});
					}else {
						this.$message({
							type: "error",
							message: res.data.result.errmsg
						});
					}
				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	/*每一项的基本信息填写*/
	.ct-list__title {
		color: #008489;
		font-size: 16px;
		margin: 1rem 0;
	}
	.ct-list__title--tip {
		color: #f60;
		font-size: 14px;
	}
	.ct-list__field {
		width: 100%;
	}
	/*提交按钮*/
	.submit {
		text-align: center;
		margin-top: 4rem;
	}
</style>
